package 分类树;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 推广奖金测试
 * @author ycc
 * @date 2024/7/26
 */
public class prize {
    public static void main(String[] args) {
        List<User> list = new ArrayList<>();
        User user1 = new User(1L, 0L, "张三");
        User user2 = new User(2L, 0L, "李四");
        User user3 = new User(3L, 0L, "王五");
        list.add(user1);
        list.add(user2);
        list.add(user3);

        List<User> list11 = new ArrayList<>();
        User user4 = new User(4L, 1L, "11");
        User user5 = new User(5L, 1L, "12");
        User user6 = new User(6L, 1L, "12");
        list11.add(user4);
        list11.add(user5);
        list11.add(user6);
        list.get(0).setChildren(list11);
        List<User> list12 = new ArrayList<>();
        User user7 = new User(7L, 5L, "121");
        User user8 = new User(8L, 5L, "122");
        list12.add(user7);
        list12.add(user8);
        list11.get(1).setChildren(list12);

        List<User> list21 = new ArrayList<>();
        User user9 = new User(9L, 2L, "21");
        User user10 = new User(10L, 2L, "22");
        list21.add(user9);
        list21.add(user10);
        list.get(1).setChildren(list21);
        List<User> list211 = new ArrayList<>();
        User user11 = new User(11L, 9L, "211");
        User user12 = new User(12L, 9L, "212");
        list211.add(user11);
        list211.add(user12);
        list21.get(0).setChildren(list211);

        System.out.println("推广奖金测试");
//        System.out.println(list);
        for(User user : list) {
            System.out.println(user);
        }

        List<User> dist = new ArrayList<>();
//        List<User> children = getChildren(list, 5L, dist);
//        getChildren3(list, 5L, dist);
//        System.out.println(dist);
//        System.out.println("----------children--------");
//        System.out.println(children);
//
//        getChildren3(list, 2L, dist);
//        System.out.println(dist);

//        System.out.println("----------temp-------------");
//        System.out.println(getChildren4(list, 5L));

        System.out.println("--------getChildren5---------");
//        List<User> children5 = getChildren5(list, 5L);
//        List<User> children5 = getChildren5(list, 9L);
//        System.out.println(children5);

//        System.out.println("--------getChildren6---------");
//        System.out.println(getChildren6(list, 5L));
//        System.out.println(getChildren6(list, 2L));

//        System.out.println(getChildren7(list, 2L));
        System.out.println(getChildren6(list, 9L));

        System.out.println("----------------测试获取node----------------");
        User node = new User();
//        getNode(list, 5L, node);
//        getNode(list, 2L, node);
        getNode(list, 9L, node);
        System.out.println(node);
    }

    public static void getNode(List<User> list, Long id, User node) {
        for (User user : list) {
            if (user.getId().equals(id)) {
//                node.setId(user.getId());
//                node.setPid(user.getPid());
//                node.setName(user.getName());
//                node.setChildren(user.getChildren());
                BeanUtils.copyProperties(user, node);
//                node = user; // 谨慎使用
//                System.out.println(node);
                return;
            } else {
                if (user.getChildren() != null) {
                    getNode(user.getChildren(), id, node); // 有无return的区别
                }
            }
        }
    }

    public static List<User> getChildren1(List<User> list, Long id, List<User> dist) {
        for(User user : list) {
            if(user.getId().equals(id)) {
                dist = user.getChildren();
                return dist;
            } else {
                if(user.getChildren() != null) {
                    return getChildren1(user.getChildren(), id, dist);
                }
            }
        }
        return dist;
    }

    public static List<User> getChildren2(List<User> list, Long id, List<User> dist) {
        for (User user : list) {
            if (user.getId().equals(id)) {
//                dist = user.getChildren();
                dist.addAll(user.getChildren());
                return dist;
            } else {
                if (user.getChildren() != null) {
                    List<User> result = getChildren2(user.getChildren(), id, dist);
                    if (result != null) {
                        dist = result;
                        return result;
                    }
                }
            }
        }
        return dist;
    }

    /**
     * 未发现bug推荐使用 - 正确版最终采用
     * @param list
     * @param id
     * @param dist
     */
    public static void getChildren3(List<User> list, Long id, List<User> dist) {
        for (User user : list) {
            if (user.getId().equals(id)) {
                dist.addAll(user.getChildren());
                return;
            } else {
                if (user.getChildren() != null && dist.size() == 0) {
                    getChildren3(user.getChildren(), id, dist);
                }
            }
        }
    }

    public static List<User> getChildren4(List<User> list, Long id) {
        List<User> temp = new ArrayList<>();
        for (User user : list) {
            if (user.getId().equals(id)) {
                temp = user.children;
                return user.getChildren();
            } else {
                if (user.getChildren() != null) {
                    return getChildren4(user.getChildren(), id);
                }
            }
        }
        return temp;
    }

    public static List<User> getChildren5(List<User> list, Long id) {
        List<User> temp = new ArrayList<>();
        for (User user : list) {
            if (user.getId().equals(id)) {
                temp = user.getChildren();
            } else {
                if (user.getChildren() != null) {
                    return getChildren5(user.getChildren(), id); // 有无return的区别
                }
            }
        }
        return temp;
    }

    public static List<User> getChildren6(List<User> list, Long id) {
        List<User> temp = new ArrayList<>();
        for (User user : list) {
            if (user.getId().equals(id)) {
                temp = user.getChildren();
                return temp;
            } else {
                if(temp.size() > 0) {
                    return temp;
                }
                if (user.getChildren() != null) {
                    getChildren6(user.getChildren(), id); // 有无return的区别
                }
            }
        }
        return temp;
    }

    public static List<User> getChildren7(List<User> list, Long id) {
        List<User> temp = new ArrayList<>();
        for (User user : list) {
            if (user.getId().equals(id)) {
                temp = user.getChildren();
                return user.getChildren();
            } else {
                if (user.getChildren() != null) {
                    getChildren7(user.getChildren(), id); // 有无return的区别
                }
            }
        }
        return temp;
    }

    private static class User {
        private Long id;
        private Long pid;
        private String name;

        private List<User> children;

        public User() {}
        public User(Long id, Long pid, String name) {
            this.id = id;
            this.pid = pid;
            this.name = name;
        }
        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public Long getPid() {
            return pid;
        }

        public void setPid(Long pid) {
            this.pid = pid;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<User> getChildren() {
            return children;
        }

        public void setChildren(List<User> children) {
            this.children = children;
        }

        @Override
        public String toString() {
//            return "User{" + "id=" + id + ", pid=" + pid + ", name='" + name + '\'' + '}';
            return "User{" + "id=" + id + ", pid=" + pid + ", name='" + name + '\'' + ", children=" + children + '}';
        }
    }
}
